package resources.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

public class Tool {
	public static int page;
	public static int max_row;
	public static File file;
	public static DateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
	public static DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd"); // use for database
	public static DateFormat format3 = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
	public static DateFormat format4 = new SimpleDateFormat("hh:mm:ss");
	public static DateFormat format5 = new SimpleDateFormat("ddMMyyhhmmss");
	public static DateFormat format6 = new SimpleDateFormat("dd-MM-yyyy");
	public static DateFormat format7 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	public static String getKey(String key){
		FacesContext context = FacesContext.getCurrentInstance();
		ResourceBundle bundle = context.getApplication().getResourceBundle(context, "msg");
		String message = bundle.getString(key);
		return message;
	}
	
	public static void initLoad(){
		try{
			FacesContext faces = FacesContext.getCurrentInstance();
			max_row = Integer.parseInt(faces.getExternalContext().getInitParameter("MAX_ROW"));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void redirect(String link){
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect(link);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static Integer[] getPages(){
		Integer[] pages = new Integer[page];
		for (int i = 0; i < pages.length; i++) {
			pages[i] = i + 1;
		}
		return pages;
	}
	
	public static String pathLog = "";
	public static PrintWriter out;
	public static void writeInfo(String text){
		try{
			pathLog = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/log/info");
			pathLog = pathLog + File.separator + "tencongty";
			file = new File(pathLog);
			if(!file.exists()){
				file.mkdir();
			}
			pathLog = file.getAbsolutePath() + File.separator + "INFO-" + format6.format(new Date()) + ".txt";
			out = new PrintWriter(new BufferedWriter(new FileWriter(pathLog, true)));
		    out.println(format3.format(new Date()) + " : " + text);
		    out.close();
		}catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
	
	public static void writeError(String text){
		try{
			pathLog = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/log/error");
			pathLog += File.separator + "ERROR-" + format6.format(new Date()) + ".txt";
			out = new PrintWriter(new BufferedWriter(new FileWriter(pathLog, true)));
		    out.println(format3.format(new Date()) + " : " + text);
		    out.close();
		}catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
	
	public static void message(String text){
		try {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			FacesMessage facesMessage = new FacesMessage(text);
			facesContext.addMessage(null, facesMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static HttpSession session(){
		try {
			return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String generateID(){			
		return format5.format(new Date());
	}
	
	public static boolean isPostBack(){		 				
		return FacesContext.getCurrentInstance().isPostback();		  
	}
	
	public static String encodeMD5(String value){		
		try{
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(value.getBytes());
			BigInteger dis = new BigInteger(1, md5.digest());
			value = dis.toString(16);
		}catch(NoSuchAlgorithmException e){
			System.out.print("Cannot encrypt MD5"); 
		}
		return value;
	}
	
}
